<template>
  <div>
    <header>
      <van-nav-bar title="支付" left-arrow @click-left="onClickLeft" />
    </header>
    <section>
      <van-password-input :value="value" info="密码为 6 位数字" :focused="showKeyboard" @focus="showKeyboard = true" @blur="showKeyboard = false"
        :show="showKeyboard" />

      <!-- 数字键盘 -->
      <van-number-keyboard :show="showKeyboard" @input="onInput" @delete="onDelete" @blur="showKeyboard = false" />
    </section>
  </div>
</template>

<script>
  import Vue from "vue";
  import {
    Dialog
  } from "vant";
	import * as api from '../api/getProlist'
  // 全局注册
  Vue.use(Dialog);
  export default {
    name: "Payment",
    data() {
      return {
        value: "",
        showKeyboard: true,
        address: ''
      };
    },

    methods: {
      onInput(key) {
        this.value = (this.value + key).slice(0, 6);
        if (this.value.length == 6) {
          if (this.value == "123456") {

            //支付成功

            console.log(this.$route.query.id)
            if (this.$route.query.id) {
              if (localStorage.getItem('chosenAddressId')) {
                api2.getOneAdd(localStorage.getItem('chosenAddressId')).then((data) => {
                  console.log(data)
                  console.log(this.$route.query.id, this.$route.query.p, data.data.address, data.data.receiver)
                  api.submitOrders({
                    receiver: data.data.receiver,
                    regions: 1 + "-" + this.$route.query.id,
                    address: data.data.address,
                    orderDetails: [{
                      quantity: 1,
                      product: this.$route.query.id,
                      price: this.$route.query.p
                    }]
                  }).then((data) => {
                    console.log(data)
                  })
                })
              }



            } else {
              this.$store.state.oderInfo.forEach(item => {
                api.submitOrders({
                    receiver: JSON.parse(localStorage.getItem('ownerinfo'))[0].receiver,
                    regions: item.quantity + "-" + item.product._id,
                    address: JSON.parse(localStorage.getItem('ownerinfo'))[0].adressDet,
                    orderDetails: [{
                      quantity: item.quantity,
                      product: item.product._id,
                      price: item.product.price
                    }]
                  })
                  .then(data => {
                    api1.deleteCart(item._id).then((data) => {
                      console.log(data)
                    })
                  });
              });
            }



            Dialog.alert({
              message: "支付成功"
            }).then(() => {
              // on close
              this.$router.push('/mine')
            });


          } else {
            Dialog.alert({
              message: "密码错误，请重新输入"
            }).then(() => {
              // on close
              this.value = "";
            });
          }
        }
      },
      onDelete() {
        this.value = this.value.slice(0, this.value.length - 1);
      },
      onClickLeft() {
        this.$toast("返回");
        this.$router.go(-1);
      }
    }
  };
</script>

<style scoped="">
  .van-password-input {
    margin-top: 200px;
  }
</style>
